package com.sssoft.Yundian.activity;

import java.util.ArrayList;

import java.util.List;

import com.readystatesoftware.viewbadger.BadgeView;
import com.sssoft.Yundian.R;
import com.sssoft.Yundian.bean.Goods;

import android.R.raw;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


/**
 * PagerAdapter 滑动菜单适配器
 * @author Jiang
 * 2017.10.11
 *
 */
@SuppressLint("NewApi")
public class MyAdapter extends PagerAdapter {
	private ImageView iv_icon1;
	private ImageView iv_icon2;
	private ImageView iv_icon3;
	private ImageView iv_icon4;
	private ImageView iv_icon5;
	private ImageView iv_icon6;
	private ImageView iv_icon7;
	private ImageView iv_icon8;
	private ImageView iv_icon9;
	private TextView tv_name1;
	private TextView tv_name2;
	private TextView tv_name3;
	private TextView tv_name4;
	private TextView tv_name5;
	private TextView tv_name6;
	private TextView tv_name7;
	private TextView tv_name8;
	private TextView tv_name9;
	private TextView tv_until1;
	private TextView tv_until2;
	private TextView tv_until3;
	private TextView tv_until4;
	private TextView tv_until5;
	private TextView tv_until6;
	private TextView tv_until7;
	private TextView tv_until8;
	private TextView tv_until9;
	private TextView tv_price1;
	private TextView tv_price2;
	private TextView tv_price3;
	private TextView tv_price4;
	private TextView tv_price5;
	private TextView tv_price6;
	private TextView tv_price7;
	private TextView tv_price8;
	private TextView tv_price9;
	private LinearLayout llItem1;
	private LinearLayout llItem2;
	private LinearLayout llItem3;
	private LinearLayout llItem4;
	private LinearLayout llItem5;
	private LinearLayout llItem6;
	private LinearLayout llItem7;
	private LinearLayout llItem8;
	private LinearLayout llItem9;

	private Context context;
	private List<View> mListViews;
	private List<Goods> goods;
	int pages; // 总页数
	private View pageView;
	private View.OnClickListener onClickListener;
	private List<ImageView> iconList;
	private List<TextView> nameList;
	private List<TextView> untilList;
	private List<TextView> priceList;
	private List<LinearLayout> itemList;
	private int[] icon = { R.id.iv_icon1, R.id.iv_icon2, R.id.iv_icon3, R.id.iv_icon4, R.id.iv_icon5, R.id.iv_icon6,
			R.id.iv_icon7, R.id.iv_icon8, R.id.iv_icon9 };

	public MyAdapter(Context context, List<Goods> goods, View.OnClickListener onClickListener) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.goods = goods;
		this.onClickListener = onClickListener;
		mListViews = new ArrayList<View>();
		initView(goods);

	}

	public void initView(List<Goods> goods) {
		// 计算ViewPager的页数
		mListViews.clear();
		pages = goods.size() / 9;
		pages = pages + 1;
		iconList = new ArrayList<ImageView>();
		nameList = new ArrayList<TextView>();
		untilList = new ArrayList<TextView>();
		priceList = new ArrayList<TextView>();
		itemList = new ArrayList<LinearLayout>();
		for (int i = 0; i < pages; i++) {
			pageView = View.inflate(context, R.layout.show_goods, null);
			Bindid(this, pageView);
			// 根据页码来填充数据
			setPagerViewData(i);
			mListViews.add(pageView);
			initListener();
		}
	}

	public void setPagerViewData(int pageNum) {
		for (int i = 0; i < 9; i++) {
			if (goods.size() > pageNum * 9 + i) {
				int temp = pageNum * 9 + i;
				Goods good = goods.get(temp);
				itemList.get(i).setVisibility(View.VISIBLE);
				iconList.get(i).setBackgroundResource(R.drawable.goods_0);
				nameList.get(i).setText(good.getGoods_name());
				untilList.get(i).setText(good.getGoods_spec());
				priceList.get(i).setText("￥ \t" + good.getGoods_price() + "/" + good.getGoods_units());
				if (good.isSelected()) {
					View target = pageView.findViewById(icon[i]);
					BadgeView badge = new BadgeView(context, target);
//					badge.setTextColor(Color.YELLOW);
					badge.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
					badge.setBadgeMargin(-1);
					badge.setText("X" + good.getSelectedNum());
					badge.show();
				}

			} else {
				itemList.get(i).setVisibility(View.INVISIBLE);
			}
		}
	}

	public void Bindid(MyAdapter myAdapter, View pageView) {
		iconList.clear();
		nameList.clear();
		untilList.clear();
		priceList.clear();
		itemList.clear();
		iv_icon1 = (ImageView) pageView.findViewById(R.id.iv_icon1);
		iv_icon2 = (ImageView) pageView.findViewById(R.id.iv_icon2);
		iv_icon3 = (ImageView) pageView.findViewById(R.id.iv_icon3);
		iv_icon4 = (ImageView) pageView.findViewById(R.id.iv_icon4);
		iv_icon5 = (ImageView) pageView.findViewById(R.id.iv_icon5);
		iv_icon6 = (ImageView) pageView.findViewById(R.id.iv_icon6);
		iv_icon7 = (ImageView) pageView.findViewById(R.id.iv_icon7);
		iv_icon8 = (ImageView) pageView.findViewById(R.id.iv_icon8);
		iv_icon9 = (ImageView) pageView.findViewById(R.id.iv_icon9);
		tv_name1 = (TextView) pageView.findViewById(R.id.tv_name1);
		tv_name2 = (TextView) pageView.findViewById(R.id.tv_name2);
		tv_name3 = (TextView) pageView.findViewById(R.id.tv_name3);
		tv_name4 = (TextView) pageView.findViewById(R.id.tv_name4);
		tv_name5 = (TextView) pageView.findViewById(R.id.tv_name5);
		tv_name6 = (TextView) pageView.findViewById(R.id.tv_name6);
		tv_name7 = (TextView) pageView.findViewById(R.id.tv_name7);
		tv_name8 = (TextView) pageView.findViewById(R.id.tv_name8);
		tv_name9 = (TextView) pageView.findViewById(R.id.tv_name9);
		tv_until1 = (TextView) pageView.findViewById(R.id.tv_until1);
		tv_until2 = (TextView) pageView.findViewById(R.id.tv_until2);
		tv_until3 = (TextView) pageView.findViewById(R.id.tv_until3);
		tv_until4 = (TextView) pageView.findViewById(R.id.tv_until4);
		tv_until5 = (TextView) pageView.findViewById(R.id.tv_until5);
		tv_until6 = (TextView) pageView.findViewById(R.id.tv_until6);
		tv_until7 = (TextView) pageView.findViewById(R.id.tv_until7);
		tv_until8 = (TextView) pageView.findViewById(R.id.tv_until8);
		tv_until9 = (TextView) pageView.findViewById(R.id.tv_until9);
		tv_price1 = (TextView) pageView.findViewById(R.id.tv_price1);
		tv_price2 = (TextView) pageView.findViewById(R.id.tv_price2);
		tv_price3 = (TextView) pageView.findViewById(R.id.tv_price3);
		tv_price4 = (TextView) pageView.findViewById(R.id.tv_price4);
		tv_price5 = (TextView) pageView.findViewById(R.id.tv_price5);
		tv_price6 = (TextView) pageView.findViewById(R.id.tv_price6);
		tv_price7 = (TextView) pageView.findViewById(R.id.tv_price7);
		tv_price8 = (TextView) pageView.findViewById(R.id.tv_price8);
		tv_price9 = (TextView) pageView.findViewById(R.id.tv_price9);
		llItem1 = (LinearLayout) pageView.findViewById(R.id.ll_item1);
		llItem2 = (LinearLayout) pageView.findViewById(R.id.ll_item2);
		llItem3 = (LinearLayout) pageView.findViewById(R.id.ll_item3);
		llItem4 = (LinearLayout) pageView.findViewById(R.id.ll_item4);
		llItem5 = (LinearLayout) pageView.findViewById(R.id.ll_item5);
		llItem6 = (LinearLayout) pageView.findViewById(R.id.ll_item6);
		llItem7 = (LinearLayout) pageView.findViewById(R.id.ll_item7);
		llItem8 = (LinearLayout) pageView.findViewById(R.id.ll_item8);
		llItem9 = (LinearLayout) pageView.findViewById(R.id.ll_item9);
		iconList.add(iv_icon1);
		iconList.add(iv_icon2);
		iconList.add(iv_icon3);
		iconList.add(iv_icon4);
		iconList.add(iv_icon5);
		iconList.add(iv_icon6);
		iconList.add(iv_icon7);
		iconList.add(iv_icon8);
		iconList.add(iv_icon9);
		nameList.add(tv_name1);
		nameList.add(tv_name2);
		nameList.add(tv_name3);
		nameList.add(tv_name4);
		nameList.add(tv_name5);
		nameList.add(tv_name6);
		nameList.add(tv_name7);
		nameList.add(tv_name8);
		nameList.add(tv_name9);
		untilList.add(tv_until1);
		untilList.add(tv_until2);
		untilList.add(tv_until3);
		untilList.add(tv_until4);
		untilList.add(tv_until5);
		untilList.add(tv_until6);
		untilList.add(tv_until7);
		untilList.add(tv_until8);
		untilList.add(tv_until9);
		priceList.add(tv_price1);
		priceList.add(tv_price2);
		priceList.add(tv_price3);
		priceList.add(tv_price4);
		priceList.add(tv_price5);
		priceList.add(tv_price6);
		priceList.add(tv_price7);
		priceList.add(tv_price8);
		priceList.add(tv_price9);
		itemList.add(llItem1);
		itemList.add(llItem2);
		itemList.add(llItem3);
		itemList.add(llItem4);
		itemList.add(llItem5);
		itemList.add(llItem6);
		itemList.add(llItem7);
		itemList.add(llItem8);
		itemList.add(llItem9);

	}

	private void initListener() {
		llItem1.setOnClickListener(onClickListener);
		llItem2.setOnClickListener(onClickListener);
		llItem3.setOnClickListener(onClickListener);
		llItem4.setOnClickListener(onClickListener);
		llItem5.setOnClickListener(onClickListener);
		llItem6.setOnClickListener(onClickListener);
		llItem7.setOnClickListener(onClickListener);
		llItem8.setOnClickListener(onClickListener);
		llItem9.setOnClickListener(onClickListener);
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		// TODO Auto-generated method stub
		container.removeView((View) object);// 删除页卡
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		// 这个方法用来实例化页
		container.addView(mListViews.get(position), 0);// 添加页卡
		return mListViews.get(position);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return pages;
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		// TODO Auto-generated method stub
		return arg0 == arg1;
	}

	/*
	 * 选中
	 */

	public void setIconAlpha(boolean isSelected, ImageView imageView) {
		if (isSelected) {
			imageView.setAlpha(1.0f);
		} else {
			imageView.setAlpha(0.4f);
		}
	}

	public void refresh(List<Goods> goods) {
		initView(goods);
		notifyDataSetChanged();
	}

	/**
	 * 使Adapter能够刷新布局
	 */
	private int mChildCount = 0;

	@Override
	public void notifyDataSetChanged() {
		mChildCount = getCount();
		super.notifyDataSetChanged();
	}

	@Override
	public int getItemPosition(Object object) {
		if (mChildCount > 0) {
			mChildCount--;
			return POSITION_NONE;
		}
		return super.getItemPosition(object);
	}

}


